package com.qf.dao.impl;

import com.qf.dao.UserDao;
import com.qf.entity.User;
import com.qf.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

public class UserDaoImpl implements UserDao {
    @Override
    public User select(String username, String password) {
        QueryRunner qr=new QueryRunner(DruidUtils.getDataSource());
        try {
            return qr.query("select * from User where username=? and password=?", new BeanHandler<>(User.class),username,password);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean selectUserName(String username) {
        QueryRunner qr=new QueryRunner(DruidUtils.getDataSource());
        try {
            User user=qr.query("select * from User where username=?", new BeanHandler<>(User.class),username);
            if(user!=null){
                return true;
            }
            return false;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void insert(User user) {
        QueryRunner qr=new QueryRunner(DruidUtils.getDataSource());
        Object[] params={user.getUsername(),user.getPassword(),user.getStatus()};
        try {
            qr.update("insert into User values(null,?,?,?);",params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
